<template>
  <el-container
    style=""
    class="space-center main-space"
  >
    <el-aside class="main-full-height">
      <el-tabs v-model="activeName">
        <el-tab-pane
          label="业务组件"
          name="second"
        >
          <div
            class="components-list"
            style="padding-bottom: 0;"
          >
            <template v-if="CostomComponents.length">
              <draggable
                tag="ul"
                :list="CostomComponents"
                v-bind="{group:{ name:'people', pull:'clone',put:false},sort:false, ghostClass: 'ghost'}"
                :move="handleMove"
                style="padding-bottom: 0;"
                filter=".forbid"
                @end="handleMoveEnd"
                @start="handleMoveStart"
              >
                <template v-for="(item, index) in CostomComponents">
                  <li
                    v-if="!(item.type=='relatebill' && schemeType==1)"
                    :key="index"
                    class="form-edit-created-label"
                    :class="{'no-put': item.type == 'divider','forbid':item.isChoose}"
                  >
                    <a :style="item.isChoose?'cursor: not-allowed':''">
                      <i
                        class="icon iconfont"
                        :class="item.icon"
                      />
                      <span>{{ item.name }}</span>
                    </a>
                  </li>
                </template>
              </draggable>
            </template>

          </div>
        </el-tab-pane>
        <el-tab-pane
          label="基础组件"
          name="first"
        >
          <div
            class="components-list"
            style="padding-bottom: 0;"
          >
            <template v-if="basicComponents.length">
              <draggable
                tag="ul"
                :list="basicComponents"
                v-bind="{group:{ name:'people', pull:'clone',put:false},sort:false, ghostClass: 'ghost'}"
                :move="handleMove"
                style="padding-bottom: 0;"
                @end="handleMoveEnd"
                @start="handleMoveStart"
              >
                <template v-for="(item, index) in basicComponents">
                  <li
                    :key="index"
                    class="form-edit-created-label"
                    :class="{'no-put': item.type == 'divider'}"
                  >
                    <a>
                      <i
                        class="icon iconfont"
                        :class="item.icon"
                      />
                      <span>{{ item.name }}</span>
                    </a>
                  </li>
                </template>
              </draggable>
            </template>

          </div>
        </el-tab-pane>
      </el-tabs>
    </el-aside>
    <el-main class="flex flex-center">

      <el-container
        class="center-container"
        direction="vertical"
        style="width:auto;max-width:350px;"
      >
        <el-main :class="{'created-empty': createdFormData.list.length == 0}">
          <ShowForm
            v-if="!resetJson"
            ref="createdFormData"
            :data="createdFormData"
            :select.sync="formDataSelect"
          />
        </el-main>
      </el-container>

    </el-main>

    <el-aside class="created-config-container">
      <el-container>
        <el-header
          height="45px"
          style="font-size: 0;"
        >
          <div
            class="config-tab"
            :class="{active: configTab=='created'}"
            @click="handleConfigSelect('created')"
          >字段属性</div>
          <div class="config-tab" :class="{active: configTab=='form'}" @click="handleConfigSelect('form')">表单属性</div>
        </el-header>
        <el-main
          class="config-content main-full-height"
          style="height:calc(100vh - 400px);overflow-x: hidden;border:none"
        >
          <config
            v-show="configTab=='created'"
            :data="formDataSelect"
          />
          <CreatedFormConfig v-show="configTab=='form'" :data="createdFormData.config" />
        </el-main>
      </el-container>
    </el-aside>
  </el-container>

</template>

<script>
import Draggable from 'vuedraggable'
import Config from './components/Config'
import ShowForm from './components/ShowForm'
import {
  basicComponents,
  layoutComponents,
  advanceComponents,
  CostomComponents
} from './config/config.js'
import code from './config/code.js'
import { deepClone } from '@/utils/index'
export default {
  name: 'MakingForm',
  components: {
    Draggable,
    Config,
    ShowForm
  },
  props: {
    editInfo: {
      type: Array,
      default: () => []
    },
    schemeType: {
      type: Number,
      default: 0
    },
    preview: {
      type: Boolean,
      default: true
    },
    generateCode: {
      type: Boolean,
      default: true
    },
    generateJson: {
      type: Boolean,
      default: true
    },
    upload: {
      type: Boolean,
      default: true
    },
    clearable: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      basicComponents,
      layoutComponents,
      advanceComponents,
      CostomComponents,
      resetJson: false,
      activeName: 'second',
      createdFormData: {
        list: [],
        config: {
          labelWidth: 100,
          labelPosition: 'right',
          size: 'small',
          customClass: ''
        }
      },
      configTab: 'created',
      formDataSelect: null,
      previewVisible: false,
      jsonVisible: false,
      remoteFuncs: {
        func_test(resolve) {
          setTimeout(() => {
            const options = [
              { id: '1', name: '1111' },
              { id: '2', name: '2222' },
              { id: '3', name: '3333' }
            ]
            resolve(options)
          }, 2000)
        },
        funcGetToken() {},
        upload_callback() {}
      },
      formDataModels: {},
      blank: '',
      htmlTemplate: '',
      jsonTemplate: '',
      uploadEditor: null,
      jsonCopyValue: '',
      jsonClipboard: null
    }
  },
  watch: {
    createdFormData: {
      deep: true,
      handler: function() {}
    },
    editInfo: {
      deep: true,
      handler() {
        if (this.editInfo) {
          this.createdFormData.list = deepClone(this.editInfo)
          this.formDataSelect =
            this.createdFormData.list.length > 0 && this.createdFormData.list[0]
        }
      }
    }
    // '$lang': function(val) {
    //   this._loadComponents()
    // }
  },
  created() {
    this._loadComponents()
    this.init()
  },
  methods: {
    init() {
      if (this.editInfo.length > 0) {
        this.createdFormData.list = deepClone(this.editInfo)
        this.formDataSelect =
          this.createdFormData.list.length > 0 && this.createdFormData.list[0]
      } else {
        this.formDataSelect =
          this.createdFormData.list.length > 0 && this.createdFormData.list[0]
      }
    },
    _loadComponents() {
      const fields = {
        input: '单行文本',
        textarea: '多行文本',
        number: '计数器',
        radio: '单选框组',
        checkbox: '多选框组',
        time: '时间选择器',
        date: '日期选择器',
        select: '下拉选择框',
        switch: '开关',
        text: '文字',
        imgupload: '图片/文件',
        grid: '栅格布局'
      }
      this.basicComponents = this.basicComponents.map(item => {
        return {
          ...item,
          name: fields[item.type]
        }
      })
      this.advanceComponents = this.advanceComponents.map(item => {
        return {
          ...item,
          name: fields[item.type]
        }
      })
      this.layoutComponents = this.layoutComponents.map(item => {
        return {
          ...item,
          name: fields[item.type]
        }
      })
    },
    handleConfigSelect(value) {
      this.configTab = value
    },
    handleMoveEnd(evt) {},
    handleMoveStart() {},
    handleMove() {
      return true
    },
    handlePreview() {
      this.previewVisible = true
    },
    handleGetData() {
      this.$refs.generateForm
        .getData()
        .then(data => {
          this.jsonVisible = true
          this.jsonTemplate = JSON.stringify(data, null, '\t')
        })
        .catch(() => {
          this.$refs.formPreview.end()
        })
    },
    handleReset() {
      this.$refs.generateForm.reset()
    },
    handleGenerateJson() {
      return this.createdFormData
    },
    handleClear() {
      this.createdFormData = {
        list: [],
        config: {
          labelWidth: 100,
          labelPosition: 'right',
          size: 'small',
          customClass: ''
        }
      }
      this.formDataSelect = {}
    },
    getJSON() {
      return this.createdFormData.list
    },
    getHtml() {
      return code(JSON.stringify(this.createdFormData))
    },
    setJSON(json) {
      this.createdFormData.list = json
      if (json.list.length > 0) {
        this.formDataSelect = json.list[0]
      }
    },
    handleInput(val) {
      this.blank = val
    },
    handleDataChange() {}
  }
}
</script>

<style lang="scss" >
@import './styles/index.scss';
.created-empty {
  background-position: 50%;
}
</style>
<style lang="scss" scoped>
.el-tabs >>> .el-tabs__nav-scroll {
  display: flex;
  justify-content: center;
}
.el-tabs__nav {
  float: none;
}
.components-list .form-edit-created-label {
  border: none;
}
.components-list ul {
  display: grid;
  grid-template-columns: 50% 50%;
  li {
    max-width: 1000px;
    padding: 5px;
    width: 100%;
    a {
      background: #ecf5ff;
      color: #409eff;
      border: 1px solid #b3d8ff;
      padding: 5px 20px;
      font-size: 14px;
      border-radius: 4px;
    }
  }
}
.main-full-height {
  border: 1px solid #eee;
  height: calc(90vh - 260px);
  overflow-y: scroll;
}
.center-container {
  height: calc(100vh - 350px);
  border-radius: 20px;
  overflow-y: hidden;
  box-shadow: 2px 2px 2px #eee;
  margin: 0 10px;
}
.created-form-container {
  overflow: hidden;
}

.created-form-container::before {
  content: '';
  height: 5px;
  width: 20%;
  display: block;
  border: 1px solid #ccc;
  border-radius: 5px;
  /* text-align: center; */
  margin-left: 40%;
  margin-top: 3px;
  position: absolute;
  z-index: 10;
}
.created-form-container::after {
  content: '';
  height: 80px;
  width: 5px;
  display: block;
  border: 1px solid #ccc;
  border-radius: 5px;
  right: -2px;
  top: 50px;
  position: absolute;
}

.created-form-container >>> .created-form-list::after {
  content: '';
  height: 40px;
  width: 5px;
  display: block;
  border: 1px solid #ccc;
  border-radius: 5px;
  right: -2px;
  top: 140px;
  position: absolute;
}
.circle-btn {
  height: 1px;
  width: 30%;
  border-radius: 20px;
  border-bottom: 1px #aaa solid;
  margin-top: 25px;

  background: #fff;
}
.el-header {
  justify-content: space-around;
  background-color: transparent;
}
.center-container .btn-bar {
  border-top: none;
}
.phone-button {
  display: flex;
  justify-content: space-around;
  position: absolute;
  bottom: 10px;
  width: 100%;
  padding: 10px 20px;
}
.main-space {
  height: calc(90vh - 260px);
  overflow-y: hidden;
  width: auto;
  max-width: 1200px;
  margin: 0 auto;
  background-color: #fefefe;
}
</style>
